package cn.lg.soar.boot.fc.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.mvc.enums.ControllerConst;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.lg.soar.boot.fc.entity.RoomFurniture;
import cn.lg.soar.boot.fc.service.RoomFurnitureService;

/**
 * 房间配套分配表
 * @author BoBo
 * @since 2025-01-20
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "房间配套分配表")
@Validated
@RestController
@RequestMapping("fc/roomFurniture")
public class RoomFurnitureController extends BaseController<RoomFurniture, RoomFurnitureService> {

    @Autowired
    private RoomFurnitureService roomFurnitureService;

    @Override
    protected Serializable getId(RoomFurniture entity) {
        return entity.getId();
    }

    @Log(value = "新增房间配套分配表", data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @ApiOperation(value = ControllerConst.CREATE_NAME)
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated(Create.class) RoomFurniture data) {
        return roomFurnitureService.addRoomFurniture(data);
    }

    @Log(value = "修改房间配套分配表", data = "#p0", dataId = "#p0.id")
    @ApiOperation(value = ControllerConst.UPDATE_NAME)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Serializable> update(@RequestBody @Validated(Update.class) RoomFurniture data) {
        return roomFurnitureService.updateRoomFurniture(data);
    }

    @Log(value = "删除房间配套分配表", data = "#p0")
    @ApiOperation(value = ControllerConst.REMOVE_NAME, notes = "根据 id 删除")
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Serializable> remove(@RequestBody List<Long> ids) {
        return roomFurnitureService.deleteRoomFurniture(ids);
    }

    @ApiOperation(value = ControllerConst.DETAIL_NAME, notes = "根据 ID 获取一条数据")
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<RoomFurniture> detail(@RequestParam Long id) {
        return roomFurnitureService.detail(id);
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "多功能查询（分页、列表、统计）")
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<RoomFurniture>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @Log(value = "导出房间配套分配表")
    @ApiOperation(value = ControllerConst.EXPORT_NAME)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @Log(value = "导入房间配套分配表")
    @ApiOperation(value = ControllerConst.IMPORT_NAME)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ApiOperation(value = "配套列表", notes = "根据 房间ID 获取配套列表")
    @RequestMapping(path = "furnitureList", method = RequestMethod.GET)
    public HttpResult<PageResult<RoomFurniture>> furnitureList(PageQuery pageQuery, @RequestParam Long roomId) {
        QueryParams params = new QueryParams();
        params.setFields(new ArrayList<>());
        List<String> f = new ArrayList<>();
        f.add("roomid-eq~"+roomId);
        List<String> orderBy = new ArrayList<>();
        orderBy.add("id");
        params.setF(f);
        params.setOrderBy(orderBy);

        return super.queryResult(pageQuery, params);
    }

    @Log(value = "拖拽排序")
    @ApiOperation(value = ControllerConst.UPDATE_NAME)
    @RequestMapping(path = "updateSort", method = RequestMethod.POST)
    public HttpResult<Serializable> updateSort(@RequestBody List<Map<String, BigInteger>> sortedData) {
        return service.updateSort(sortedData);
    }

}